<template>
  <div class="lo">
  <img src="https://img1.baidu.com/it/u=1825851994,4163570429&fm=253&fmt=auto&app=120&f=JPEG?w=934&h=500.jpg" alt="" class="img"/>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username" class="lp">
        <el-input type="user" v-model="ruleForm.username" class="ipt"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" class="lp">
        <el-input type="password" v-model="ruleForm.password" class="ipt"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        <!-- <el-button @click="resetForm('ruleForm')"></el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { login } from "../../api/user";

export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          login(this.ruleForm).then(res => {
            console.log(res);
            localStorage.setItem("token", res.data);
            this.$router.push("/");
            this.$message(res.message);
          });
        } else {
          this.$message(res.message);
        }
      });
    }
  }
};
</script>
<style lang="scss">
.io{
  position: relative;
}
.ipt{
  width: 100px;
 
}
.demo-ruleForm{
  position: absolute;
  top: 20%;
  left: 600px;
  width: 300px;
  height: 300px;
  background: #fff;
 
}
.img{
  width: 100%;
  height: 100%;
}
.lp{
  margin-top: 50px;
  margin-right: 30px;
}
</style>